package mobi.zeedev.flyingsaucer.common.DataModule;

import android.content.ContentUris;
import android.net.Uri;
import android.net.Uri.Builder;
import android.provider.BaseColumns;

public class FlyingSaucerConv {

    public static final String AUTHORITY = "com.zeedev.provider.FlyingSaucer";

    public static final String GROUP_BY = "groupBy";
    public static final String TASTED_BY = "tastedBy";
    public static final String ACTIVE_BY = "activeBy";
    public static final String SORT_BY = "sortBy";
    public static final String FILTER_BREWERS = "filterBrewers";
    public static final String FILTER_STYLES = "filterStyles";
    public static final String FILTER_COUNTRIES = "filterCountries";
    public static final String FILTER_CONTAINER = "filterContainer";

    public static final String BY_STORE_DB_ID = "byStoreDbId";

    public static final String BY_MEMBER_ID = "byMemberId";

    public static final String BY_MEMBER_DB_ID = "byMemberDbId";

    public static final String ADD_STANDARD_MEMBER_BEER_COLUMNS = "addStandardMemberBeerColumns";

    public static final String ADD_STANDARD_STORE_BEER_COLUMNS = "addStandardStoreBeerColumns";


    public static final class Members implements BaseColumns {

        public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/members");

        public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.flyingsaucer.member";

        public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.flyingsaucer.member";

        public static final String DEFAULT_SORT_ORDER = _ID + " DESC";

        public static final String MEMBERS_KEY = "members_key";

        public static final String PASSWORD = "memberPassword";
        public static final String STORE_NUMBER = "memberStoreNumber";
        public static final String CARD_NUMBER = "memberCardNumber";
        public static final String PASSWORD_HASH = "memberPasswordHash";

        public static final String UFO_LOGIN_REMEMBER = "ufoLoginRemember";
        public static final String UFO_LOGIN_AUTO_LOGIN = "ufoLoginAutoLogon";
        public static final String UFO_LOGIN_NON_MEMBER = "ufoLoginNonMember";
//		public static final String UFO_PREFERRED_STORE = "ufoPreferredStore";
//		public static final String UFO_HOME_CACHE_DB_ID = "ufoHomeCacheDbId";

        public static final String UFO_HOME_NUMBER_BEERS = "ufoHomeNumberBeers";
        public static final String UFO_HOME_NUMBER_TOTAL_BEERS = "ufoHomeNumberTotalBeers";
        public static final String UFO_HOME_USERNAME = "ufoHomeUserName";
        public static final String UFO_HOME_NAME = "memberName";

        public static final String UFO_MEMBER_ID = "ufoMemberId";
        public static final String UFO_ORIGIN_DATE = "ufoOriginDate";
        public static final String UFO_MOU_MEMBER = "ufoMouMember";

        public static final String UFO_IS_CURRENT_USER = "ufoIsCurrentUser";

        public static final String CREATION_DATE = "ufoMemberCreationDate";
        public static final String UPDATED_DATE = "ufoMemberUpdatedDate";
        public static final String BEERS_REFRESHED_DATE = "members_beer_refreshed_date";

//		public static final String MEMBERS_TABLE = "members";

        public static final String TABLE = "members";

        public static final String g(String child) {
            return FlyingSaucerConv.g(TABLE, child);
        }

        public static final Uri withId(String storeDbId) {
            return ContentUris.withAppendedId(Members.CONTENT_URI, Long.valueOf(storeDbId));
        }

        public static final class MemberBeer implements BaseColumns {
            public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/members/beers");

            public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.flyingsaucer.memberbeer";
            public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.flyingsaucer.memberbeer";

            public static final Uri FILTER_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/members/beers/filters");

            public static final String TABLE = "memberbeers";
            public static final String DEFAULT_SORT_ORDER = _ID + " desc";

            //Relationships
            public static final String MEMBER_BEER_KEY = "member_beer_key";
            public static final String BEER_DB_ID = "memberBeerBeerDbId";
            public static final String MEMBER_DB_ID = "memberBeerMemberDbId";

            //Fields
            public static final String CREATION_DATE = "memberbeer_creation_date";
            public static final String UPDATED_DATE = "memberbeer_updated_date";
            public static final String ACTIVE = "member_beer_active";
            public static final String COMMENTS = "comments";
            public static final String RATING = "rating";
            public static final String TASTED_DATE = "dateTasted";

            //virtual columns
            public static final String IS_TASTED = "memberbeer_isTasted";
            public static final String SIMPLE_TASTED_DATE = "memberbeer_asSimpleTastedDate";
            public static final String SIMPLE_CREATION_DATE = "memberbeer_asSimpleCreationDate";
//			public static final String NEW_BEERS = "memberbeer_isNewBeer";


/*            public static final class Styles implements BaseColumns {
                public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.flyingsaucer.memberbeer.style";
                public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.flyingsaucer.style";
                public static final String DEFAULT_SORT_ORDER = FlyingSaucerConv.Beers.STYLE_NAME + " DESC";
                public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/members/beers/styles");

                public static final Uri withId(String storeDbId) {
                    Uri t = Util.buildUriWithSecondPath(Members.CONTENT_URI, storeDbId, "beers");
                    return t.buildUpon().appendPath("styles").build();

                }

            }

            public static final class Brewers implements BaseColumns {
                public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.flyingsaucer.memberbeer.brewer";
                public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.flyingsaucer.brewer";
                public static final String DEFAULT_SORT_ORDER = FlyingSaucerConv.Beers.BREWER_NAME + " DESC";
                public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/members/beers/brewers");

                public static final Uri withId(String storeDbId) {
                    Uri t = Util.buildUriWithSecondPath(Members.CONTENT_URI, storeDbId, "beers");
                    return t.buildUpon().appendPath("brewers").build();

                }

            }

            public static final class Dates implements BaseColumns {
                public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.dir/vnd.flyingsaucer.memberbeer";
                public static final String CONTENT_TYPE = "vnd.android.cursor.item/vnd.flyingsaucer.memberbeer";
                public static final String DEFAULT_SORT_ORDER = FlyingSaucerConv.Members.MemberBeer.SIMPLE_TASTED_DATE + " desc";
                public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/members/beers/dates");

                public static final Uri withId(String identKey) {
                        Uri ret = null;
                        Builder b = new Builder();
                        b.scheme("content");
                        b.authority(AUTHORITY);
                        b.appendPath("members");
                        b.appendPath(identKey);
                        b.appendPath("beers");
                        b.appendPath("dates");
                        ret = b.build();
                        return ret;
                    }

            }*/


//			public static final String INTERNAL_DATE_ADDED = "internalDateAdded";
//			public static final String DB_ID = "_id";
//			public static final String STATUS = "status";
//			public static final String DATE_ADDED_RAW = "dateAddedRaw";
//			public static final String SIMPLE_DATE = "simpleDate";
//			public static final String TASTED = "tasted";
//			public static final String MEMBER_ID = "memberId";

            public static final String g(String child) {
                return FlyingSaucerConv.g(TABLE, child);
            }


            public static final Uri withMemberId(String identKey) {
                Uri ret = null;
                Builder b = new Builder();
//        		b.authority(AUTHORITY);
//        		b.appendPath("members");
                b.scheme("content");
                b.authority(AUTHORITY);
                b.appendPath("members");
                b.appendPath(identKey);
//        		b.appendQueryParameter("members", identKey);
                b.appendPath("beers");


                ret = b.build();
                return ret;
            }

            public static final Uri withMemberBeerId(String identKey) {
                Uri ret = null;
                Builder b = new Builder();
//        		b.authority(AUTHORITY);
//        		b.appendPath("members");
                b.scheme("content");
                b.authority(AUTHORITY);
                b.appendPath("members");
//        		b.appendQueryParameter("members", identKey);
                b.appendPath("beers");
                b.appendPath(identKey);


                ret = b.build();
                return ret;
            }

            public static final String[] STANDARD_COLUMNS = {
                    MemberBeer.MEMBER_BEER_KEY,
                    Beers.BEER_ID,
                    Beers.NAME, Beers.CONTAINER, Beers.CAN, Beers.BOTTLE, Beers.DRAUGHT
            };


            public static final String getMemberBeersJoin() {
/*				String members_id_beers_join = 	
			    	Beers.TABLE + " INNER JOIN " + TABLE + " ON (" + 
			    	BEER_DB_ID + " = " + 
			    	Beers.g(Beers.BEERS_KEY) +
			    	") " +
			    	"INNER JOIN " + TABLE + " ON (" +
			    	MEMBER_DB_ID + " = " + Members.g(Members.MEMBERS_KEY) + ")" 
			    	;*/

                String members_id_beers_join =
                        MemberBeer.TABLE + " INNER JOIN " + Beers.TABLE + " ON (" +
                                BEER_DB_ID + " = " +
                                Beers.BEERS_KEY +
                                ") " +
                                "INNER JOIN " + Members.TABLE + " ON (" +
                                MEMBER_DB_ID + " = " + Members.MEMBERS_KEY + ")";
                return members_id_beers_join;
            }

        }

    }

    public static final class Stores implements BaseColumns {

        public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/stores");
        public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.flyingsaucer.store";
        public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.flyingsaucer.store";

        public static final String DEFAULT_SORT_ORDER = Stores.NAME + " ASC";

        public static final String TABLE = "stores";

        public static final String STORES_KEY = "stores_key";

        public static final String NAME = "storeName";
        public static final String URL_NAME = "storeUrlName";
        public static final String URL = "storeUrl";
        public static final String NUMBER = "storeNumber";


        public static final String CREATION_DATE = "stores_creation_date";
        public static final String UPDATED_DATE = "stores_updated_date";

        public static final String g(String child) {
            return FlyingSaucerConv.g(TABLE, child);
        }

        public static final Uri withId(String storeDbId) {
            return ContentUris.withAppendedId(Stores.CONTENT_URI, Long.valueOf(storeDbId));
        }

        public static final class StoreBeer implements BaseColumns {
            public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.flyingsaucer.storebeer";
            public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.flyingsaucer.storebeer";

            public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/stores/beers");
            public static final Uri FILTER_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/stores/beers/filter");

            public static final String STORE_BEER_KEY = "store_beer_key";
            public static final String BEER_DB_ID = "storebeer_beerDbId";
            public static final String STORE_DB_ID = "storebeer_storeDbId";
            //virtual
//	        	public static final String VIRTUAL_STORE_DB_ID = "BeersStoresStoreNumber";
//	        	public static final String VIRTUAL_BEER_DB_ID = "BeersStoresBeerId";

            public static final String TABLE = "storebeer";

            public static final String DEFAULT_SORT_ORDER = _ID + " DESC";//FlyingSaucerConv.Beers.NAME + " DESC";

            public static final String CREATION_DATE = "store_beer_creation_date";
            public static final String UPDATED_DATE = "store_beer_updated_date";

            public static final String ACTIVE = "store_beer_active";

            //VIRTUAL COLUMNS
            public static final String SIMPLE_CREATION_DATE = "storebeer_simple_creation_date";

            public static final String[] STANDARD_COLUMNS = {
                    STORE_BEER_KEY,
                    ACTIVE,
                    FlyingSaucerConv.Beers.BEER_ID,
                    FlyingSaucerConv.Beers.NAME, FlyingSaucerConv.Beers.CONTAINER,
                    FlyingSaucerConv.Beers.CAN, FlyingSaucerConv.Beers.BOTTLE, FlyingSaucerConv.Beers.DRAUGHT
            };

            public static final String g(String child) {
                return FlyingSaucerConv.g(TABLE, child);
            }

            public static final Uri withId(String storeDbId) {
                return Util.buildUriWithSecondPath(Stores.CONTENT_URI, storeDbId, "beers");
            }

/*	            public static final class Styles implements BaseColumns {
                    public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.flyingsaucer.beer.style";
                    public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.flyingsaucer.style";
                    public static final String DEFAULT_SORT_ORDER = FlyingSaucerConv.Beers.STYLE_NAME + " DESC";
                    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/stores/beers/styles");

                    public static final Uri withStoreDbId(String storeDbId) {
                        Uri t = Util.buildUriWithSecondPath(StoresPreferencesModel.CONTENT_URI, storeDbId, "beers");
                        return t.buildUpon().appendPath("styles").build();

                    }

                }

                public static final class Brewers implements BaseColumns {
                    public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.flyingsaucer.beer.brewer";
                    public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.flyingsaucer.brewer";
                    public static final String DEFAULT_SORT_ORDER = FlyingSaucerConv.Beers.BREWER_NAME + " DESC";
                    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/stores/beers/brewers");

                    public static final Uri withStoreDbId(String storeDbId) {
                        Uri t = Util.buildUriWithSecondPath(StoresPreferencesModel.CONTENT_URI, storeDbId, "beers");
                        return t.buildUpon().appendPath("brewers").build();
                    }
                }*/

            public static final String getStoreBeerJoin() {
                String join =
                        StoreBeer.TABLE + " INNER JOIN " + Beers.TABLE + " ON (" +
                                BEER_DB_ID + " = " +
                                Beers.BEERS_KEY +
                                ") " +
                                "INNER JOIN " + Stores.TABLE + " ON (" +
                                STORE_DB_ID + " = " + Stores.STORES_KEY + ")";

                return join;
            }


        }

    }

    public static final class Beers implements BaseColumns {
        // This class cannot be instantiated
        private Beers() {
        }

//        public static final String BEERS_TABLE_NAME = ;

        public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/beers");

        public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.flyingsaucer.beer";

        public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.flyingsaucer.beer";

        public static final String DEFAULT_SORT_ORDER = Beers.NAME + " ASC";

        public static final String TABLE = "beers";

        public static final String g(String child) {
            return FlyingSaucerConv.g(TABLE, child);
        }

/*        public static final class Styles implements BaseColumns {
    public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.flyingsaucer.beer.style";
    public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.flyingsaucer.style";
    public static final String DEFAULT_SORT_ORDER = Beers.STYLE_NAME + " DESC";
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/beers/styles");
}

public static final class Brewers implements BaseColumns {
    public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.flyingsaucer.beer.brewer";
    public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.flyingsaucer.brewer";
    public static final String DEFAULT_SORT_ORDER = Beers.BREWER_NAME + " DESC";
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/beers/brewers");
}*/
        //FIELDS

        public static final String BEERS_KEY = "beers_key";

        public static final String BEER_ID = "beerId";
        public static final String COUNTRY_ID = "countryId";
        public static final String BREWER_ID = "brewerId";
        public static final String STYLE_ID = "styleId";

        public static final String RAW_NAME = "rawName";
        // child
        public static final String NAME = "name";
        public static final String CAN = "can";
        public static final String NAME_SUBSCRIPT = "nameSubscript";

        public static final String CONTAINER = "container";
        // child
        public static final String BOTTLE = "draught";
        public static final String DRAUGHT = "bottle";

        public static final String DESCRIPTION = "description";
        public static final String CITY = "city";
        public static final String COUNTRY_NAME = "countryName";
        public static final String STYLE_NAME = "styleName";
        public static final String BREWER_NAME = "brewerName";
        public static final String AVERAGE_RATING = "averageRating";

        // DB Layered

        // Modified Externally only
//		public static final String INTERNAL_DATE_ADDED = "internalDateAdded";
//		public static final String DB_ID = "_id";
//		public static final String STATUS = "status";

        // Internally Modified
//		public static final String COMMENTS = "comments";
//		public static final String RATING = "rating";

//		public static final String DATE_ADDED = "dateAdded";
        // child
        // public static final String DATE_ADDED_RAW = "dateAddedRaw";
//		public static final String SIMPLE_DATE = "simpleDate";
//		public static final String TASTED = "tasted";

//		public static final String MEMBER_ID = "memberId";

        //NON-COUNTED, VIRTUAL PROPERTIES
//		public static final String SIMPLE_INTERNAL_DATE = "simpleInternalDate"; 
//		public static final String NEW_SIMPLE_DATE = "newSimpleDate";
//		public static final String SIMPLE_INTERNAL_DATE_2 = "simpleInternalDate2";
//		public static final String DESCRIPTION_VALID = "descriptionValid";
//		public static final String GROUP_BY = "groupBy";

        public static final String[] BEER_LIST_COLUMNS = {
                Beers.BEERS_KEY,
                Beers.BEER_ID,
                Beers.NAME, Beers.CONTAINER, Beers.CAN, Beers.BOTTLE, Beers.DRAUGHT
        };
    }



    //Utility functions

    public static final String g(String tableName, String cur) {
        return Util.mergeWithPeriod(tableName, cur);
    }




}
